package com.myexample.computerintenet;

import com.myexample.computerintenet.util.MyInfo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {
    public static void main(String[] args) {
        /*try {
            new MyServer();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        System.out.println(Integer.MIN_VALUE);
    }

    public int divide(int dividend, int divisor) {
        if (dividend == 0) {
            return dividend;
        }
        if (divisor == 1)
            return dividend;
        if (divisor == -1) {
            if (dividend > Integer.MIN_VALUE)
                return -dividend;
            return Integer.MAX_VALUE;
        }
        if (dividend < 0 && divisor < 0) {
            int count = 0;
            while (dividend - divisor <= 0) {
                count++;
                dividend -= divisor;
            }
            return count;
        } else if (dividend > 0 && divisor > 0) {
            int count = 0;
            while (dividend - divisor >= 0) {
                count++;
                dividend -= divisor;
            }
            return count;
        } else {
            int count = 0;
            while (Math.abs(dividend) - Math.abs(divisor) >= 0) {
                count++;
                dividend += divisor;
            }
            return -count;
        }
    }

    public MyServer() throws IOException {

        ServerSocket serverSocket = new ServerSocket(MyInfo.PORT);
        while (true) {

            System.out.println("-----等待客户端连接-----");
            Socket socket = serverSocket.accept();//等待连接
            if (socket.isConnected()) {
                System.out.println("客户端连接: " + socket.getInetAddress().getHostAddress());

                String text = getText(socket);
                System.out.println(text);

                System.out.println("------信息接收完成------");

                if (text != null)
                    sendText(socket,/*"客户端发送了： "+*/text.toUpperCase());

                if (socket.isClosed()) {
                    System.out.println("-----等待客户端连接-----");
                }
            }
        }
    }

    private String getText(Socket socket) {
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (inputStream != null) {
            StringBuilder builder = new StringBuilder();
            /*
            try {
                if(inputStream.available()==0)
                    return null;
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String str = null;
            while (true) {
                try {
                    if ((str = reader.readLine()) == null)
                        break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                builder.append(str);
            }
            //尝试关闭输入流
            try {
                socket.shutdownInput();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return builder.toString();
        }
        return null;
    }

    private void sendText(Socket socket, String text) {
        if (socket != null && !socket.isClosed()) {
            OutputStream outputStream = null;
            try {
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (outputStream != null) {
                PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream));
                out.write(text);
                out.flush();
                //out.close();
            }
            //尝试关闭输出流
            try {
                socket.shutdownOutput();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
